/**
 * Created by qianjy on 2016/12/2.
 */
require.config(window.jshead);
require(['jquery','layer','navigator'], function ($,layer) {
    layer.config({
        path: '/js/layer/'
    });
    //图片验证码
    $('.validImg').click(function() {
        $(".validImg").attr("src", "/sys/ImageServlet?time=" + new Date());
    });
    $('#clickMes').bind('click',function(){
        var phone=$('#phone').val();
        var validNum=$("#code").val();
        if(phone==""){
            layer.tips("请先输入手机号",'#phone');
            return;
        }
        //短信接口
        time($('#clickMes'));
        $.ajax({
            type: "POST",
            url: "/doLoginSendSms",
            data: {
                mobile:phone,
                pictureCode: validNum
            },
            dataType: "json",
            success: function (data) {
                if(data.result=='success'){
                    layer.msg("短信已发送！");
                }else{
                    wait=0;
                    layer.msg(data.messageText);
                }
            }
        });
        //短信接口end
    });

    $('#login').bind('click',function(){
        var phone=$('#phone').val();
        var mobileCode=$('#mobileCode').val();
        var validNum=$("#code").val();
        if(phone==""){
            layer.tips("请先输入11位手机号",'#phone');
            return;
        }
        if (!/^[1][3,4,5,7,8][0-9]{9}$/.exec(phone)) {
            layer.tips("请输入正确的手机号",'#phone');
            return;
        }
        if(mobileCode==""){
            layer.tips("请先输入验证码",'#mobileCode');
            return;
        }
        if(validNum==""){
            layer.tips("请输入正确的验证码",'#code');
            return;
        }
        var index = layer.load(1, {
            shade: [0.1,'#fff'] //0.1透明度的白色背景
        });
        //图片验证
        $(".validImg").attr("src", "/sys/ImageServlet?time=" + new Date());
        $.ajax({
            type: "POST",
            url: "/login/quickLogin",
            data: {
                mobile:phone,
                mobileCode:mobileCode,
                pictureCode: validNum
            },
            dataType: "json",
            success: function (data) {
                layer.close(index);
                if(data.result=='success'){
                    window.location.href="/user/assets";
                }else{

                    layer.alert(data.messageText);
                }
            }
        });
    })
    var wait=60;
    function time(o) {
        if (wait == 0) {
            o.removeAttr("disabled");
            o.val('获取验证码');
            o.css("color", '#ffffff');
            o.css("background","#2c97f4");
            wait = 60;
            $('.validImg').trigger('click')
        } else {
            o.attr("disabled", true);
            o.css("color", '#fff');
            o.css("background", '#ddd');
            o.val("重新发送(" + wait + "s)");
            wait--;
            setTimeout(function() {
                    time(o)
                },
                1000)
        }
    }
    $(".login-content").keydown(function() {
        if (event.keyCode == "13") {//keyCode=13是回车键
            $('#login').click();
        }
    });
//用户名输入后可点击删除
    $("#phone").bind("input propertychange",function(){
        var phone =$('#phone').val();
        var userDel = $(".login_input_wrap .user_del");
        if(phone){
            userDel.css({"display":"block"});
            userDel.bind("click", function (){
                $('#phone').val("");
                userDel.css({"display":"none"});
            })
        }else{
            userDel.css({"display":"none"});
        }
    })







});